<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公告标题" prop="F002V">
        <el-input
          v-model="queryParams.F002V"
          placeholder="请输入公告标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery(editableTabsValue)">搜索
        </el-button>
      </el-form-item>
    </el-form>

    <el-tabs v-model="editableTabsValue" type="border-card" @tab-click="handleTabClick">
      <el-tab-pane v-for="option in keyword"
                   :key="option.id"
                   :label="option.typeName"
                   :value="option.typeName">
        <el-table v-loading="loading" :data="announcementsList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="日期" align="center" prop="RECTIME">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.RECTIME, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="证券代码	" align="center" prop="SECCODE"/>
          <el-table-column label="证券简称" align="center" prop="SECNAME"/>
<!--          <el-table-column label="公告日期" align="center" prop="F001D" width="180">-->
<!--            <template slot-scope="scope">-->
<!--              <span>{{ parseTime(scope.row.F001D, '{m}-{d}') }}</span>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="公告标题" :show-overflow-tooltip='true' min-width="400" align="center" prop="F002V">
            <template slot-scope="scope">
          <span class="ellipsis"  @click="viewPdf(scope.row.F003V)" style="color: #409EFF; cursor: pointer;">
            {{ scope.row.F002V }}
          </span>
            </template>
          </el-table-column>
          <el-table-column label="时间" align="center" prop="RECTIME" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.RECTIME, '{h}:{m}') }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  listAnnouncements,
  getAnnouncements,
  delAnnouncements,
  addAnnouncements,
  updateAnnouncements
} from "@/api/system/announcements";
import {listKeyword} from "@/api/system/keyword";

export default {
  name: "Announcements",
  data() {
    return {
      tabIndex: 0,
      editableTabsValue: '0',
      pdfUrl: "",
      keyword: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // announcements表格数据
      announcementsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        parentName: null,
        pageNum: 1,
        pageSize: 10,
        RECID: null,
        SECCODE: null,
        SECNAME: null,
        F001D: null,
        F002V: null,
        F003V: null,
        F004V: null,
        F005N: null,
        F006V: null,
        F007V: null,
        F008V: null,
        F009V: null,
        F010V: null,
        OBJECTID: null,
        RECTIME: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        RECID: [
          {required: true, message: "主体ID不能为空", trigger: "blur"}
        ],
        SECCODE: [
          {required: true, message: "证券代码	不能为空", trigger: "blur"}
        ],
        SECNAME: [
          {required: true, message: "证券简称不能为空", trigger: "blur"}
        ],
        F001D: [
          {required: true, message: "公告日期不能为空", trigger: "blur"}
        ],
        F002V: [
          {required: true, message: "公告标题不能为空", trigger: "blur"}
        ],
        F003V: [
          {required: true, message: "公告地址不能为空", trigger: "blur"}
        ],
        F004V: [
          {required: true, message: "公告格式不能为空", trigger: "blur"}
        ],
        F005N: [
          {required: true, message: "公告大小不能为空", trigger: "blur"}
        ],
        F006V: [
          {required: true, message: "信息分类不能为空", trigger: "blur"}
        ],
        F007V: [
          {required: true, message: "证券类别编码不能为空", trigger: "blur"}
        ],
        F008V: [
          {required: true, message: "证券类别名称不能为空", trigger: "blur"}
        ],
        F009V: [
          {required: true, message: "证券市场编码不能为空", trigger: "blur"}
        ],
        F010V: [
          {required: true, message: "证券市场名称不能为空", trigger: "blur"}
        ],
        OBJECTID: [
          {required: true, message: "OBJECTID不能为空", trigger: "blur"}
        ],
        RECTIME: [
          {required: true, message: "发布时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getListKeyword()
    this.getList();
  },
  methods: {
    removeTab(targetName) {
      console.log(targetName)
      // 查找当前标签的索引
      const tabs = this.keyword;
      let activeName = this.editableTabsValue;

      // 如果当前选中的标签被删除，切换到其他标签
      if (activeName === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.typeName === targetName) {
            const nextTab = tabs[index + 1] || tabs[index - 1];
            if (nextTab) {
              activeName = nextTab.typeName;
            }
          }
        })
      }

      // 过滤掉删除的标签
      this.keyword = tabs.filter(tab => tab.typeName !== targetName);

      // 更新当前选中的标签
      this.editableTabsValue = activeName;

      // 如果所有标签被移除，则重置查询参数
      if (this.keyword.length === 0) {
        this.queryParams.parentName = null;
        this.getList();
      }
    },
    handleTabClick(tab) {
      // 根据标签的名称或其他属性做进一步处理
      this.queryParams.parentName = tab.label
      this.queryParams.F002V = null
      this.getList();
    },
    viewPdf(pdfUrl) {
      // 检查 PDF 地址是否有效
      if (pdfUrl) {
        // 使用 window.open 打开 PDF
        window.open(pdfUrl, '_blank');
      } else {
        this.$message.warning('无效的公告地址');
      }
    },
    /** 查询关键字配置列表 */
    getListKeyword() {
      listKeyword({parentId: 0}).then(response => {
        this.keyword = response.data;
        console.log(this.keyword[0].typeName)
        this.queryParams.parentName = this.keyword[0].typeName
        this.getList();
      });
    },
    /** 查询announcements列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.F002V) {
        this.queryParams.parentName = null
      }
      listAnnouncements(this.queryParams).then(response => {
        this.announcementsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        TEXTID: null,
        RECID: null,
        SECCODE: null,
        SECNAME: null,
        F001D: null,
        F002V: null,
        F003V: null,
        F004V: null,
        F005N: null,
        F006V: null,
        F007V: null,
        F008V: null,
        F009V: null,
        F010V: null,
        OBJECTID: null,
        RECTIME: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      let name = this.queryParams.F002V;
      this.queryParams.pageNum = 1;

      // 检查是否已有相同 title 的标签
      const isDuplicate = this.keyword.some(option => option.typeName === name);
      if (isDuplicate) {
        this.$message.error("标签已经存在，无法添加重复标签！");
        return; // 如果是重复标签，直接返回
      }

      // 创建新的标签
      let newTabName = (++this.tabIndex)+1 + ''; // 使用 tabIndex 自增来生成新的标签名

      // 为新标签设置 title 和 name
      this.keyword.push({
        typeName: name,        // 使用输入的公告标题作为标签的名称
        name: newTabName    // 使用自增的 tabIndex 来作为标签的唯一标识符
      });

      // 更新选中的标签
      this.editableTabsValue = newTabName;

      console.log("更新后的keyword:", this.keyword);
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.TEXTID)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const TEXTIDs = row.TEXTID || this.ids;
      this.$modal.confirm('是否确认删除announcements编号为"' + TEXTIDs + '"的数据项？').then(function () {
        return delAnnouncements(TEXTIDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/announcements/export', {
        ...this.queryParams
      }, `announcements_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.ellipsis {
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 隐藏超出部分 */
  text-overflow: ellipsis; /* 使用省略号表示超出部分 */
  display: block;
}

</style>
